package com.caiheng.api.service.estate.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caiheng.api.dao.estate.ReceptionDayMsgMapper;
import com.caiheng.api.dao.estate.ReceptionDayMsgRecordMapper;
import com.caiheng.api.dao.wxuser.UserMapper;
import com.caiheng.api.entity.estate.Estate;
import com.caiheng.api.entity.estate.ReceptionDayMsg;
import com.caiheng.api.entity.estate.ReceptionDayMsgRecord;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.exception.TransactionalException;
import com.caiheng.api.service.estate.ReceptionDayMsgService;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.vdurmont.emoji.EmojiParser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 物业经理接待日Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-13
 */
@Service
public class ReceptionDayMsgServiceImpl extends BaseServiceImpl<ReceptionDayMsgMapper, ReceptionDayMsg> implements ReceptionDayMsgService {

    @Resource
    private ReceptionDayMsgMapper msgMapper;
    @Resource
    private ReceptionDayMsgRecordMapper msgRecordMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    @Transactional
    public int addMsg(User user, Estate estate, String msg) {
        int ret = 0;
        try{
            ReceptionDayMsg dayMsg = new ReceptionDayMsg();
            dayMsg.setContent(msg);
            dayMsg.setReadState(0);
            dayMsg.setCreateTime(new Date());
            dayMsg.setEstateId(estate.getId());
            dayMsg.setEstateName(estate.getEstateName());
            dayMsg.setUserId(user.getId());
            ret = msgMapper.insert(dayMsg);
            if(ret<=0){
                throw new TransactionalException("msgMapper insert error");
            }

            //发送 物业经理的推送通知

        }catch (Exception e){
            e.printStackTrace();
        }
        return ret;
    }

    @Override
    public ReceptionDayMsg getMsgById(int msgId) {
        ReceptionDayMsg dayMsg = msgMapper.selectById(msgId);
        if(dayMsg != null){
            QueryWrapper<ReceptionDayMsgRecord> qw = new QueryWrapper<>();
            qw.eq("reception_id",dayMsg.getId());
            qw.eq("parent_id", 0);
            List<ReceptionDayMsgRecord> msgRecords = msgRecordMapper.selectList(qw);
            if(CollUtil.isNotEmpty(msgRecords)){
                for (ReceptionDayMsgRecord record : msgRecords){

                    User user = userMapper.selectById(record.getUserId());
                    if(user != null){
                        if(StrUtil.isNotEmpty(user.getNickName())){
                            record.setUserNickName(EmojiParser.parseToUnicode(user.getNickName()));
                        }
                        record.setUserFaceUrl(user.getFaceUrl());
                    }


                    User manager = userMapper.selectById(record.getManagerWxUserId());
                    if(manager != null){
                        if(StrUtil.isNotEmpty(manager.getNickName())){
                            record.setManagerNickName(EmojiParser.parseToUnicode(manager.getNickName()));
                        }
                        record.setManagerFaceUrl(manager.getFaceUrl());
                    }


                    QueryWrapper<ReceptionDayMsgRecord> qw1 = new QueryWrapper<>();
                    qw1.eq("reception_id",dayMsg.getId());
                    qw1.eq("parent_id", record.getId());
                    List<ReceptionDayMsgRecord> msgRecordChilds = msgRecordMapper.selectList(qw);
                    if(CollUtil.isNotEmpty(msgRecordChilds)){
                        for (ReceptionDayMsgRecord recordChild : msgRecordChilds){

                            user = userMapper.selectById(recordChild.getUserId());
                            if(user != null){
                                if(StrUtil.isNotEmpty(user.getNickName())){
                                    recordChild.setUserNickName(EmojiParser.parseToUnicode(user.getNickName()));
                                }
                                recordChild.setUserFaceUrl(user.getFaceUrl());
                            }


                            manager = userMapper.selectById(recordChild.getManagerWxUserId());
                            if(manager != null){
                                if(StrUtil.isNotEmpty(manager.getNickName())){
                                    recordChild.setManagerNickName(EmojiParser.parseToUnicode(manager.getNickName()));
                                }
                                recordChild.setManagerFaceUrl(manager.getFaceUrl());
                            }
                        }
                    }
                    record.setRecordChild(msgRecordChilds);
                }
            }
        }
        return dayMsg;
    }
}
